package com.example.sams.controller;

import com.example.sams.domain.request.EnrollCourseRequest;
import com.example.sams.domain.request.UpdateProfileRequest;
import com.example.sams.service.IStudentsService;
import com.example.sams.utils.LoginUser;
import com.example.sams.utils.ResultUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author jinghong
 * @since 2025-11-04
 */
@RestController
@RequestMapping("/students")
public class StudentsController {

    @Autowired
    private IStudentsService studentsService;

    /**
     * 获取当前学生信息
     * GET /students/me
     */
    @GetMapping("/me")
    public ResultUtil getCurrentStudentInfo() {
        // 从请求属性中获取当前登录用户ID（由JwtInterceptor设置）
        String userId = LoginUser.getCurrentId();
        if (userId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return studentsService.getCurrentStudentInfo(userId);
    }

    /**
     * 获取当前学生已选课程列表
     * GET /students/me/courses
     */
    @GetMapping("/me/courses")
    public ResultUtil getMyCourses() {
        // 从请求属性中获取当前登录用户ID（由JwtInterceptor设置）
        String userId = LoginUser.getCurrentId();
        if (userId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return studentsService.getMyCourses(userId);
    }

    /**
     * 选课
     * POST /students/me/courses
     */
    @PostMapping("/me/courses")
    public ResultUtil enrollCourse(@RequestBody EnrollCourseRequest request) {
        // 从请求属性中获取当前登录用户ID（由JwtInterceptor设置）
        String userId = LoginUser.getCurrentId();
        if (userId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return studentsService.enrollCourse(userId, request);
    }

    /**
     * 退课
     * DELETE /students/me/courses/{courseId}
     */
    @DeleteMapping("/me/courses/{courseId}")
    public ResultUtil dropCourse(@PathVariable String courseId) {
        // 从请求属性中获取当前登录用户ID（由JwtInterceptor设置）
        String userId = LoginUser.getCurrentId();
        if (userId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return studentsService.dropCourse(userId, courseId);
    }

    /**
     * 更新个人资料
     * PATCH /students/me
     */
    @PatchMapping("/me")
    public ResultUtil updateProfile(@RequestBody UpdateProfileRequest request) {
        // 从请求属性中获取当前登录用户ID（由JwtInterceptor设置）
        String userId = LoginUser.getCurrentId();
        if (userId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return studentsService.updateProfile(userId, request);
    }

    /**
     * 根据教师名称获取教师信息
     * GET /students/teachers/{teacherName}
     */
    @GetMapping("/teachers/{teacherName}")
    public ResultUtil getTeacherByName(@PathVariable String teacherName) {
        try {
            // URL解码教师名称
            String decodedTeacherName = java.net.URLDecoder.decode(teacherName, java.nio.charset.StandardCharsets.UTF_8);
            return studentsService.getTeacherByName(decodedTeacherName);
        } catch (Exception e) {
            // 如果解码失败，直接使用原始名称
            return studentsService.getTeacherByName(teacherName);
        }
    }
}
